var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    is_show:false,
    showTopTipsText: '',
    order_id:0,
    order_list:{},
    address:'',
    invoice_no:'',
    agent_type:0,
    index:0,
    kuaidi:'',
    express_list:[],
    btn_show:false,//发货按钮 禁用/可用
    hostimgs: app.globalData.hostimgs,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //登录调用
    getApp().getUserInfo(function (userInfo, openid1) {

    })
    
    var that = this;
    var order_id = options.order_id;
    that.setData({
      order_id:order_id,
      agent_type: app.globalData.agent_type,
    })
    wx.showLoading({
      title: '加载中',
    })
    // 调用订单详情
    get_order_detail(that, order_id) 
  },

  bindPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    var list = e.currentTarget.dataset.list;
    //console.log(list[e.detail.value])
    this.setData({
      index: e.detail.value,
      kuaidi: list[e.detail.value],//快递公司名称
    })
  },
  //点击发货
  fahuo: function (e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    var order_status = 2;//已发货
    var invoice_no = that.data.invoice_no;
    var shipping_name = that.data.kuaidi;//快递公司名称

    if (invoice_no == '' || invoice_no == undefined ) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入快递编号',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      if (shipping_name == '' || shipping_name == undefined){
        shipping_name = that.data.express_list[0]
      }
      this.setData({
        order_id: order_id,
        btn_show:true,
      })
      //修改订单状态
      modi_order_status(that, order_id, 1, app.globalData.user_id, invoice_no, shipping_name)

    }

  },
  // 快递编号 文本框输入事件
  onclickinvoice_no: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      invoice_no: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },


})


//订单详情接口
function get_order_detail(that, order_id) {

  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Order/get_order_detail.html',
    data: {
      order_id: order_id,
    },
    method: 'GET',
    header: { 'content-type': 'application/json' }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        order_list:res.data,
        express_list: res.data.express_list,
        is_show:true,
      })

    },
    fail: function () { },
    complete: function () {
      wx.hideLoading()
     }
  })
}


//修改订单状态
function modi_order_status(that, order_id, oprate_code, user_id, invoice_no, shipping_name) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Order/modi_order_status.html',
    data: {
      order_id: order_id,
      oprate_code: oprate_code,
      user_id: user_id,
      invoice_no: invoice_no,
      shipping_name: shipping_name,

    },
    method: 'GET',
    header: { 'content-type': 'application/json' }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == 2){
        //成功提示
        wx.showModal({
          title: '提示',
          content: '发货成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')

              wx.redirectTo({
                url: '../order/orderlist',
              })
              that.setData({
                btn_show: false,
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{

      }

    },
    fail: function () { },
    complete: function () {

    }
  })
}